<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$gameLastIssue=Yii::app()->request->getParam('issue');
		if(!isset($gameLastIssue)){
			$gameLastIssue=GameInfo::model()->findAllBySql("select distinct issue from tbl_gameinfo order by issue desc limit 0, 1", array(), array());
			$gameLastIssue=$gameLastIssue[0]->issue;
		}
		
		$lstModel=array();
		if(isset($gameLastIssue)){
			$lstModel=VWGameInfo::model()->findAll(
				array(
					'select'=>'*',
					'condition'=>'issue=:issue',
					'params'=>array(':issue'=>$gameLastIssue),
					)
				);
		}
		$gameIssue=GameInfo::model()->findAllBySql("select distinct issue from tbl_gameinfo order by issue desc limit 0, 20", array(), array());
		
		$gameTypeList=GameInfo::model()->findAllBySql("select distinct gametype from tbl_gameinfo where issue=:issue", array(':issue'=>$gameLastIssue));

		$this->render('index',array(
			'model'=>$lstModel,
			'issue'=>$gameIssue,
			'currentIssue'=>$gameLastIssue,
			'gameTypeList'=>$gameTypeList,
			)
		);
	}

	public function actionQuery()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$gid=Yii::app()->request->getParam('gid');
		$qtype=Yii::app()->request->getParam('qtype');
		if(!isset($qtype)){
			$qtype="1";
		}
		$lstModel=array();
		if(isset($gid)){
			$game=GameInfo::model()->find(
				array(
					'select'=>'*',
					'condition'=>'gameid=:gameid',
					'params'=>array(':gameid'=>$gid),
					)
				);
			if(isset($game)){
				if($qtype=="1"){
					//威廉希尔相同赔率（五大联赛）
					$lstModel=VWGameInfo::model()->findAll(
					array(
						'select'=>'*',
						'condition'=>"euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and gametime<:gametime",
						'params'=>array(':euroodds3w'=>$game['euroodds3w'],
										':euroodds1w'=>$game['euroodds1w'],
										':euroodds0w'=>$game['euroodds0w'],
										':gametime'=>$game['gametime'],),
						'order'=>'gametime desc',
						)
					);
				}
				else if($qtype=="2"){
					//威廉希尔相同赔率（相同联赛）
					$lstModel=VWGameInfo::model()->findAll(
					array(
						'select'=>'*',
						'condition'=>"euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and gametype=:gametype and gametime<:gametime",
						'params'=>array(':euroodds3w'=>$game['euroodds3w'],
										':euroodds1w'=>$game['euroodds1w'],
										':euroodds0w'=>$game['euroodds0w'],
										':gametype'=>$game['gametype'],
										':gametime'=>$game['gametime'],),
						'order'=>'gametime desc',
						)
					);
				}
				else if($qtype=="3"){
					//第一赔率相同所有场次（五大联赛）
				}
				else if($qtype=="4"){
					//第一赔率相同所有场次（相同联赛）
				}
				$this->render('query',array(
					'model'=>$lstModel,
					'game'=>$game,
					)
				);
			}
		}
	}

	public function actionIndex1()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$gameLastIssue=Yii::app()->request->getParam('issue');
		if(!isset($gameLastIssue)){
			$gameLastIssue=GameInfo::model()->findAllBySql("select distinct issue from tbl_gameinfo order by issue desc limit 0, 1", array(), array());
			$gameLastIssue=$gameLastIssue[0]->issue;
		}
		
		$lstModel=array();
		if(isset($gameLastIssue)){
			$lstModel=VWGameInfo::model()->findAll(
				array(
					'select'=>'*',
					'condition'=>'issue=:issue',
					'params'=>array(':issue'=>$gameLastIssue),
					)
				);
		}
		$gameIssue=GameInfo::model()->findAllBySql("select distinct issue from tbl_gameinfo order by issue desc limit 0, 20", array(), array());
		
		$gameTypeList=GameInfo::model()->findAllBySql("select distinct gametype from tbl_gameinfo where issue=:issue", array(':issue'=>$gameLastIssue));

		$this->render('index',array(
			'model'=>$lstModel,
			'issue'=>$gameIssue,
			'currentIssue'=>$gameLastIssue,
			'gameTypeList'=>$gameTypeList,
			)
		);
	}

	public function actionHelp()
	{
		$this->render('help',array());
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	public function actionQueryOdds()
	{
		$gameid=Yii::app()->request->getParam('gameid');
		$companyid=Yii::app()->request->getParam('companyid');
		if(isset($gameid) && isset($companyid)){
			$gameOdds=OddsInfo::model()->findAllBySql("select * from tbl_oddsinfo where gameid=:gameid and companyid=:companyid", array(':gameid'=>$gameid, ':companyid'=>$companyid, ), array());
			$arr3=array();
			$arr1=array();
			$arr0=array();
			$time=array();
			foreach ($gameOdds as $key => $value) {
				array_push($arr3, floatval($value->odds3));
				array_push($arr1, floatval($value->odds1));
				array_push($arr0, floatval($value->odds0));
				array_push($time, $value->writetime);
			}
			$arr3=json_encode($arr3);
			$arr1=json_encode($arr1);
			$arr0=json_encode($arr0);
			$time=json_encode($time);

			$this->render('queryodds',array(
				'model'=>$gameOdds,
				'arr3'=>$arr3,
				'arr1'=>$arr1,
				'arr0'=>$arr0,
				'time'=>$time,
				)
			);
		}
		else{
			$this->render('queryodds',array());
		}
	}

	public function actionQueryBfyl()
	{
		$issue=Yii::app()->request->getParam('issue');
		$gameindex=Yii::app()->request->getParam('gameindex');
		if(isset($issue) && isset($gameindex)){
			$bfOddsInfo=BfOddsInfo::model()->findAllBySql("select * from tbl_bfoddsinfo
				where gameid in
				(
					select go.gameid from tbl_gameinfo as g,
					tbl_gameinfoOkooo as go
					where g.issue=go.issue 
					  and g.gameindex=go.gameindex 
					  and g.issue=:issue
					  and g.gameindex=:gameindex
				)", array(':issue'=>$issue, ':gameindex'=>$gameindex, ), array());
			$arr3=array();
			$arr1=array();
			$arr0=array();
			$time=array();
			$arr3exchange=array();
			$arr1exchange=array();
			$arr0exchange=array();
			$arr3yl=array();
			$arr1yl=array();
			$arr0yl=array();
			foreach ($bfOddsInfo as $key => $value) {
				array_push($arr3, floatval($value->bfprice3));
				array_push($arr1, floatval($value->bfprice1));
				array_push($arr0, floatval($value->bfprice0));
				array_push($time, $value->writetime);
				array_push($arr3exchange, floatval($value->bfexchange3));
				array_push($arr1exchange, floatval($value->bfexchange1));
				array_push($arr0exchange, floatval($value->bfexchange0));
				array_push($arr3yl, floatval($value->yk3));
				array_push($arr1yl, floatval($value->yk1));
				array_push($arr0yl, floatval($value->yk0));
			}
			$arr3=json_encode($arr3);
			$arr1=json_encode($arr1);
			$arr0=json_encode($arr0);
			$time=json_encode($time);
			$arr3exchange=json_encode($arr3exchange);
			$arr1exchange=json_encode($arr1exchange);
			$arr0exchange=json_encode($arr0exchange);
			$arr3yl=json_encode($arr3yl);
			$arr1yl=json_encode($arr1yl);
			$arr0yl=json_encode($arr0yl);

			//查询比赛详情
			$gameInfo=GameInfo::model()->findAllBySql("select * from tbl_gameinfo
				where issue=:issue
				  and gameindex=:gameindex", array(':issue'=>$issue, ':gameindex'=>$gameindex, ), array());

			$this->render('querybfyl',array(
				'model'=>$bfOddsInfo,
				'arr3'=>$arr3,
				'arr1'=>$arr1,
				'arr0'=>$arr0,
				'time'=>$time,
				'arr3exchange'=>$arr3exchange,
				'arr1exchange'=>$arr1exchange,
				'arr0exchange'=>$arr0exchange,
				'arr3yl'=>$arr3yl,
				'arr1yl'=>$arr1yl,
				'arr0yl'=>$arr0yl,
				'gameInfo'=>$gameInfo,
				)
			);
		}
		else{
			$this->render('querybfyl',array());
		}
	}

	public function actionQueryMacau()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$gid=Yii::app()->request->getParam('gid');
		$lstModel=array();
		if(isset($gid)){
			$game=GameInfo::model()->find(
				array(
					'select'=>'*',
					'condition'=>'gameid=:gameid',
					'params'=>array(':gameid'=>$gid),
					)
				);
			if(isset($game)){
				$lstModel=VWGameInfo::model()->findAll(
					array(
						'select'=>'*',
						'condition'=>"odds3m=:odds3m and odds1m=:odds1m and odds0m=:odds0m and gametime<:gametime",
						'params'=>array(':odds3m'=>$game['odds3m'],
										':odds1m'=>$game['odds1m'],
										':odds0m'=>$game['odds0m'],
										':gametime'=>$game['gametime'],),
						'order'=>'gametime desc',
						)
					);
				$this->render('querymacau',array(
					'model'=>$lstModel,
					'game'=>$game,
					)
				);
			}
		}
	}

	public function actionQueryMacauAsia()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$gid=Yii::app()->request->getParam('gid');
		$lstModel=array();
		if(isset($gid)){
			$game=GameInfo::model()->find(
				array(
					'select'=>'*',
					'condition'=>'gameid=:gameid',
					'params'=>array(':gameid'=>$gid),
					)
				);
			if(isset($game)){
				$lstModel=VWGameInfo::model()->findAll(
					array(
						'select'=>'*',
						'condition'=>"oddshomemacau=:oddshomemacau and ratemacau=:ratemacau and oddsawaymacau=:oddsawaymacau and gametime<:gametime",
						'params'=>array(':oddshomemacau'=>$game['oddshomemacau'],
										':ratemacau'=>$game['ratemacau'],
										':oddsawaymacau'=>$game['oddsawaymacau'],
										':gametime'=>$game['gametime'],),
						'order'=>'gametime desc',
						)
					);
				$this->render('querymacau',array(
					'model'=>$lstModel,
					'game'=>$game,
					)
				);
			}
		}
	}
}